package com.hjqjl.module_choice.widget.dialogfragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.hjqjl.module_choice.databinding.ChoiceDialogCustomBinding;

//自定义view的Dialog

/**
 * DialogCustomFragment dialogCustomFragment = DialogCustomFragment.newInstance
 * ("我是title", "我是内容--我是自定义的DialogFragment"
 * , new DialogCustomFragment.OnClickMyListener() {
 *
 * @Override public void onMyClick(Dialog dialog) {
 * ToastUtils.showToast(mContext, "我是内容--我是自定义的DialogFragment");
 * dialog.dismiss();
 * }
 * });
 * dialogCustomFragment.show(getSupportFragmentManager(), "customDialog");
 */
public class DialogCustomFragment extends DialogFragment {
    private ChoiceDialogCustomBinding binding;
    private OnClickMyListener listener;

    /**
     * Listener
     */
    public interface OnClickMyListener {
        void onMyClick(Dialog dialog);
    }

    private DialogCustomFragment(OnClickMyListener listener) {
        this.listener = listener;
    }

    public static DialogCustomFragment newInstance(String title, String message, OnClickMyListener listener) {
        DialogCustomFragment fragment = new DialogCustomFragment(listener);
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("message", message);
        fragment.setArguments(bundle);
        return fragment;
    }

    @NonNull
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = ChoiceDialogCustomBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        String title = "";
        String message = "";
        if (bundle != null) {
            title = bundle.getString("title");
            message = bundle.getString("message");
        }
        binding.tvDialogfragmentTitle.setText(title);
        binding.tvDialogfragmentMessage.setText(message);
        binding.tvDialogfragmentConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onMyClick(getDialog());
                }
            }
        });
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
